import java.util.*;
import java.io.*;

import org.eclipse.swt.widgets.Display;

public class FileSystemWatcher {
	
	static public FileSystemWatcher create(){
		return null;
		
	}
	
	//tempo entre polling em ms
	int deltaTime;
	//path das pastas para polling
	String movementPath;
	String attackPath;
	
	//conjunto observers
	ArrayList<Observer> observers;
	
	FileSystemWatcher(int deltaTime,String movementPath,String attackPath, Display display){
		this.deltaTime=deltaTime;
		this.movementPath=movementPath;
		this.attackPath=attackPath;
		this.observers = new ArrayList<Observer>();
		
		//thread para os movimentos
		TimerTask taskMovement;
		display.asyncExec(
				taskMovement=new DirWatcher(movementPath,deltaTime,display) {
					protected void onChange( File file, String action ) {
						String name = file.getName();
						notifyObserversMovement(name.substring(0, name.lastIndexOf(".")),file.getPath(),action);
					}
				});
		//thread para os attacks
		TimerTask taskAttack;
		display.asyncExec(
				taskAttack = new DirWatcher(attackPath,deltaTime,display) {
					protected void onChange( File file, String action ) {
						String name = file.getName();
						notifyObserversAttack(name.substring(0, name.lastIndexOf(".")),file.getPath(),action);
					}
				});
		
		
		display.timerExec(deltaTime, taskMovement);
		display.timerExec(deltaTime, taskAttack);
	}
	
	void registerObserver(Observer o){
		observers.add(o);
	}
	
	void notifyObserversAttack(String name, String path,String action){
		for(Observer o: observers){
			o.notifyAttack(name,path,action);
		}
	}
	void notifyObserversMovement(String name, String path,String action){
		for(Observer o: observers){
			o.notifyMovement(name,path,action);
		}
	}

}
